<?php
require 'include/global.php';
require_once 'include/train_db.php';
$smarty->compile_check = true;
$newpw = myf(6);



if($_POST[username]){
	$uname = trim($_POST[username]);
	$email = trim($_POST[useremail]);
	$sql_1  = " select ep_email from pa_employer where ep_uname = '".$uname."' AND `ep_email`='".$email."' ";
	//echo $sql_1;
	$arr1 = $DB->fetch_one_array($sql_1);
	$sql_2  = " select js_email from pa_candidates where js_uname ='".$uname."' AND `js_email`='".$email."'";
	//echo $sql_2;
	$arr2 = $DB->fetch_one_array($sql_2);
	$sql_3  = " select rc_email from pa_recuiter where rc_uname ='".$uname."' AND `rc_email`='".$email."'";
	//echo $sql_3;
	$arr3 = $DB->fetch_one_array($sql_3);
	$sql_4  = " select ad_email from pa_advisory where ad_uname ='".$uname."' AND `ad_email`='".$email."'";
	//echo $sql_3;
	$arr4 = $DB->fetch_one_array($sql_4);

	if(empty($arr1)&&empty($arr2)&&empty($arr3)&&empty($arr4)){
		$err = "Username or Email is not valid!";
		$smarty->assign("err",$err);
	}else{
		if(!empty($arr1)){
           $em = $arr1[ep_email];
		   $DB->query("UPDATE pa_employer SET `ep_pword`='".md5($newpw)."' WHERE  ep_uname = '".$uname."' AND `ep_email`='".$email."'");
		}
		if(!empty($arr2)){
		   $em = $arr2[js_email];
		   $DB->query("UPDATE pa_candidates SET `js_pword`='".md5($newpw)."' WHERE  js_uname ='".$uname."' AND `js_email`='".$email."'");
		}
		if(!empty($arr3)){
		   $em = $arr3[rc_email];
		   $DB->query("UPDATE pa_recuiter SET `rc_pword`='".md5($newpw)."' WHERE rc_uname ='".$uname."' AND `rc_email`='".$email."'");
		}
		if(!empty($arr4)){
		   $em = $arr4[ad_email];
		   $DB->query("UPDATE pa_advisory SET `ad_pword`='".md5($newpw)."' WHERE ad_uname ='".$uname."' AND `ad_email`='".$email."'");
		}

		$to = $em;
		$content = "<html>
				<head>
				  <title>Contact Us</title>
				</head>
				<body>
				<p>Here is your temporary password <font color=red>".$newpw."</font> <br />
					To change your password: Go to > My Account > My Profile > Enter a new password > Click Enter and Save.</p>
				<p>From The enetRecruiter Support Team<br />
				<a href='mailto:Support@enetrecruiter.com'>Support@enetrecruiter.com</a></p>
				</body>
				</html>";
		$subject  = "Your Password".$_POST[contactsubject];
		$headers  = "MIME-Version: 1.0" . "\r\n";
		$headers .= "Content-type: text/html; charset=utf-8" . "\r\n";
		$headers .= "From: enetRecruiter <info@enetrecruiter.com>" . "\r\n";
		
		@mail($to, $subject, $content, $headers);
		echo "<script>alert('Your new password has been send successfully,thank you.');
		window.location.href='index.php';
		</script>";

	}

}

$smarty->display('header.tpl');
$smarty->display('left.tpl');
$smarty->display('forget.tpl');
$smarty->display('right.tpl');
$smarty->display('footer.tpl');
?>
